package com.iemes.controller.craft;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import net.sf.json.util.NewBeanInstanceStrategy;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.InspectionItemFormMap;
import com.iemes.entity.ProcessCondFormMap;
import com.iemes.entity.ProcessRecordsFormMap;
import com.iemes.entity.ProcessTestFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.TechnoParameterFormMap;
import com.iemes.entity.TechnoSettingFormMap;
import com.iemes.entity.TechnologyFormMap;
import com.iemes.mapper.craft.InspectionItemMapper;
import com.iemes.mapper.craft.ProcessCondMapper;
import com.iemes.mapper.craft.ProcessRecordsMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
@Controller
@RequestMapping("condition")
public class ProcessCondController extends BaseController{
	@Inject
	private ProcessCondMapper processCondMapper;
	@Inject
	private ProcessRecordsMapper recorsMapper;
	@Inject 
	private InspectionItemMapper inspectionItemMapper;
	@RequestMapping(value = "records", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/technology/condition/records_list";
	}

	@ResponseBody
	@RequestMapping("records_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		ProcessCondFormMap processCondFormMap = getFormMap(ProcessCondFormMap.class);
		processCondFormMap.put("status", 1);
		List<Map<String,Object>> processCond = processCondMapper.getProcessCond(processCondFormMap);
		try {
			LuogicPaging(pageNow, pageSize, processCond);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}
	// 查看工艺  
	@RequestMapping(value = "records_edit")
	public String craft_edit(Model model) {
		InspectionItemFormMap inspectionItemFormMap = new InspectionItemFormMap();
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			ProcessCondFormMap processCond = processCondMapper.findbyFrist("id", id, ProcessCondFormMap.class);
			ProcessRecordsFormMap processRecords = recorsMapper.findbyFrist("id",processCond.get("ProcessRecordsId").toString(), ProcessRecordsFormMap.class);
			inspectionItemFormMap.put("idOmo", processCond.get("Omolding"));
			 inspectionItemFormMap.put("idTmo", processCond.get("Tmolding"));
			 inspectionItemFormMap.put("idSmo", processCond.get("Smolding"));
			 inspectionItemFormMap.put("idFmo", processCond.get("Fmolding"));
			 inspectionItemFormMap.put("idFivemo", processCond.get("Fivemolding"));
			List<Map<String,Object>> inspectionItem = inspectionItemMapper.getInspectionItem(inspectionItemFormMap);
			model.addAttribute("processCond", processCond);
			model.addAttribute("processRecords", processRecords);
			model.addAttribute("inspection", inspectionItem);
		}
		if (Integer.valueOf(getPara("status")) == 1) {
			return Common.BACKGROUND_PATH + "/technology/condition/records_editUpd";
		} else {
			return Common.BACKGROUND_PATH + "/technology/condition/records_edit";
		}
	}
		
		//修改工艺操作
		@ResponseBody
		@RequestMapping("records_editEntity")
		@SystemLog(module="成型工艺条件检查",methods="成型工艺条件检查-修改工艺")//凡需要处理业务逻辑的.都需要记录操作日志
		@Transactional
		public String editEntity() throws Exception {
			//修改工艺为添加最新工艺，保存原来工艺，即为新增工艺，保存日期不一样
			ProcessCondFormMap processCondFormMap =getFormMap(ProcessCondFormMap.class);
			ProcessRecordsFormMap processRecordsFormMap = getFormMap(ProcessRecordsFormMap.class);
			InspectionItemFormMap inspectionItemFormMap = new InspectionItemFormMap();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			processCondFormMap.put("Record_date", sdf.format(new Date()));
			processRecordsFormMap.put("Date", sdf.format(new Date()));
			try {
				processCondMapper.editEntity(processCondFormMap);
				recorsMapper.editEntity(processRecordsFormMap);
			for (int i = 1; i < 6; i++) {		
				inspectionItemFormMap.put("id", getPara("inspectionItemFormMap.id"+i));
				inspectionItemFormMap.put("Steam",getPara("inspectionItemFormMap.Steam"+i));
				inspectionItemFormMap.put("Vacuum", getPara("inspectionItemFormMap.Vacuum"+i));
				inspectionItemFormMap.put("HighPressure",getPara("inspectionItemFormMap.HighPressure"+i));
				inspectionItemFormMap.put("LowPressure", getPara("inspectionItemFormMap.LowPressure"+i));
				inspectionItemMapper.editEntity(inspectionItemFormMap);
			}
			} catch (Exception e) {
				return "false";
			}
			
			return "success";
		}
		//删除工艺操作
		@ResponseBody
		@RequestMapping("records_deleteEntity")
		@Transactional(readOnly=false)//需要事务操作必须加入此注解
		@SystemLog(module="成型工艺条件检查",methods="成型工艺条件检查-删除工艺")//凡需要处理业务逻辑的.都需要记录操作日志
		public String deleteEntity() {
			String[] ids = getParaValues("ids");
			String arr = ids[0];
			String[] str = arr.split(",");
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ProcessCondFormMap processCondFormMap = new ProcessCondFormMap();
				for (int i = 0; i < str.length; i++) {
						//先查询子表的ID，修改子表后再修改父表		
					try {
						processCondFormMap.put("id", str[i]);
						processCondFormMap.put("status", 2);
						processCondFormMap.put("Record_date",sdf.format(new Date()));
						processCondMapper.editEntity(processCondFormMap);
						
					} catch (Exception e) {
							return "false";
				}				
			}
				return "success";
		}
}
